package com.pignest.hotel.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.InitializingBean;

import com.pignest.hotel.beans.Card;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.beans.RoomType;
import com.pignest.hotel.dao.RoomDao;
import com.pignest.hotel.dao.RoomTypeDao;
import com.pignest.hotel.definition.RoomLiveStatus;
import com.pignest.hotel.definition.RoomRepairStatus;
import com.pignest.hotel.definition.RoomUseStatus;
import com.pignest.hotel.system.Logic;
import com.pignest.hotel.util.DateUtil;

public class RoomService extends BaseService implements InitializingBean {

	private RoomDao roomDao;
	private RoomTypeDao roomTypeDao;
//	private RoomStatusDao roomStatusDao;
	
	public RoomType getRoomType(String id) {
		return roomTypeDao.get(RoomType.class, id);
	}
	
	public RoomType getRoomTypeByName(String name) {
		return roomTypeDao.getRoomTypeByName(name);
	}
	
	 
	public void addRoomType(RoomType type) {
		roomTypeDao.insert(type);
	}
	
	public void updateRoomType(RoomType type) {
		roomTypeDao.update(type);
	}
	
	public void removeRoomType(RoomType type) {
		roomTypeDao.delete(type);
	}
	
	public List<RoomType> listRoomTypes() {
		return roomTypeDao.listAll(RoomType.class);
	}
	
	/////////////////////////////////////////////////////////
	public Room getRoom(String id) {
		return roomDao.get(Room.class, id);
	}
	
	public void addRoom(Room room) {
		roomDao.insert(room);
	}
	
	public Room addRoom(String name, String type, String floor) {
		Room room = new Room();
		room.setName(name);
		room.setType(getRoomTypeByName(type));
		room.setRoomUseStatus(RoomUseStatus.DEFAULT);
		room.setRoomLiveStatus(RoomLiveStatus.DEFAULT);
		room.setRoomRepairStatus(RoomRepairStatus.DEFAULT);
		room.setFloor(floor);
		addRoom(room);
		return room;
	}
	
	public void updateRoom(Room room) {
		roomDao.update(room);
	}
	
	/**
	 * 夜审时把所有房间状态更新为脏房
	 * */
	public void updateUseStatus() {
		//超过24小时的空干净 改为 空隔夜
		String hql = "update com.pignest.hotel.beans.Room set useStatus=:p1 where useStatus=:p2 and liveStatus=:p3 and (lastCleanTime is null or lastCleanTime<:p4)";
		roomDao.updateByHQL(hql, new String[] {"p1", "p2", "p3", "p4"}, new Object[] {RoomUseStatus.EMPTY_NIGHT.name(), RoomUseStatus.CLEAN.name(), RoomLiveStatus.EMPTY.name(), new Date(System.currentTimeMillis() - DateUtil.ONE_DAY)});
		//在住净 改为 在住脏
		hql = "update com.pignest.hotel.beans.Room set useStatus=:p1 where liveStatus=:p2";
		roomDao.updateByHQL(hql, new String[] {"p1", "p2"}, new Object[] {RoomUseStatus.DIRTY.name(), RoomLiveStatus.FILLED.name()});
	}
	
	public void removeRoom(Room room) {
		roomDao.delete(room);
	}
	
	public List<Room> listAllRooms() {
		return roomDao.listAll(Room.class);
	}
	
	public List<Room> listRooms(RoomType... roomTypes) {
		return roomDao.listRooms(roomTypes);
	}
	public List<Room> listRooms(RoomLiveStatus liveStatus,RoomType... roomTypes){
		return roomDao.listRooms(liveStatus, roomTypes);
	}
	public List<Room> listRooms(String... roomTypeIds){
		if (roomTypeIds == null || roomTypeIds.length == 0)
			return listAllRooms();
		RoomType[] types = new RoomType[roomTypeIds.length];
		return listRooms(types);
	}
	
	public List<Room> listRooms(List<RoomType> roomTypes){
		if (roomTypes == null || roomTypes.size() == 0)
			return listRooms(new String[0]);
		String[] ids = new String[roomTypes.size()];
		for (int i=0, j=roomTypes.size(); i<j; i++) {
			ids[i] = roomTypes.get(i).getId();
		}
		return listRooms(ids);
	}
	
	public boolean hasRooms(String... roomTypeIds) {
		return roomDao.hasRooms(roomTypeIds);
	}
	
	/** 通过房间号获取房间 */
	public Room getRoomByName(String roomName) {
		return roomDao.getRoomByName(roomName);
	}
	
	public Room getRoomByLock(String lockNumber) {
		return get(Room.class, "lockNumber", lockNumber);
	}
	
	public String getRoomLockByName(String roomName){
		return roomDao.getRoomLockByName(roomName);
	}
	
	public List<Room> listSelfHelpRooms(String roomTypeId) {
		return roomDao.listSelfHelpRooms(roomTypeId);
	}
	
	public List<Room> listBookingRooms() {
		return roomDao.listBookingRooms();
	}
	
	/////////////////////////////////////////////////////////
	
	public void setRoomDao(RoomDao roomDao) {
		this.roomDao = roomDao;
	}
	public void setRoomTypeDao(RoomTypeDao roomTypeDao) {
		this.roomTypeDao = roomTypeDao;
	}
//	public void setRoomStatusDao(RoomStatusDao roomStatusDao) {
//		this.roomStatusDao = roomStatusDao;
//	}

	@Override
	public void afterPropertiesSet() throws Exception {
		//初始化数据
	}
	
	/** 
	 * 是否可以入住
	 * @deprecated 使用 {@link Logic#canLiving(Room)}
	 * */
	public static boolean canLiving(Room room) {
		return Logic.canLiving(room);
	}
	
	/**
	 * 是否可以退房
	 * 该房间必须 是已经入住的情况
	 * @deprecated 使用 {@link Logic#canCheckOut(Room)}
	 * */
	public static boolean canCheckOut(Room room){
		return Logic.canLiving(room);
	}
	
	/**
	 * 验证房卡是否有效性
	 * */
	public String checkCard(Card card,Living l)
    {
    	if(card==null)
		{
			return "无效卡！";
		}
		if(card.getLockNumber()==null)
			return "系统检测卡表无对应锁号！请确认！";
		Room r = this.getRoomByLock(card.getLockNumber());
		if(r==null)
		{
			return "此卡无对应房间！";
		}else
		{
			if(!r.getName().equals(l.getRoom().getName())){
				return "系统检测此卡打开的是"+r.getName()+"房间！与目前"+l.getRoom().getName()+"不一致!";
			}
			
		}
		return null;
    }
  
}
